﻿/// <reference path="jquery.js" />
/// <reference path="svg.js" />
/// <reference path="pv.core.js" />
/// <reference path="database.js" />

(function ($) {
    $.fn.createTarget = function (type, position, rotation, opts) {
        var $target = $(this);

        opts = $.extend({ attacked: function (demage, sender) { } }, opts || {});

        var targetType = getType(type, targets);

        var node = $(this).createNode(targetType, 'target', position.x, position.y, $.extend({ rotation: rotation }, opts || {}));

        var defaultStatus = $.extend({}, node.status);

        var hp = targetType.hp_max;
        //attached
        node.bind('hit', function (event, sender, e) {
            var realDemage = -sender.status.demage + node.status.armor;
            realDemage = hp > realDemage ? realDemage : hp;
            if (realDemage < 0) {
                hp += realDemage;
                tip.sound("sound/h2.mp3");

                tip.fadeOut(realDemage, node);

                node.status.attached(node, {});

                if (hp <= 0) {

                    node.dispose();
                    return;
                }
            }

            var buff = sender.status.effect(sender, e);
            if (buff)
                effect(buff);
        });

        //move
        pv.setTimer(node, null, function () {
            var rotation = pv.attachPath(node, $target);
            radianrotation = rotation * Math.PI / 180;
            var diff = { x: Math.sin(radianrotation), y: (radianrotation == 0 ? -1 : Math.cos(radianrotation) * -1) };
            diff = geo.vectorNorm(diff);
            var direction = { x: node.status.speed * diff.x, y: node.status.speed * diff.y };

            position.x = position.x + direction.x;
            position.y = position.y + direction.y;

            if (pv.isInAttchRange(position, node.status.radius)) {
                beginAttack();
                return true;
            }
            else {
                node.setTranslate(position.x, position.y);
                node.setRotate(rotation);
            }
        });

        //#region accumulating buff
        var accumulateList = [];
        function accumulate(id) {
            var l = accumulateList.length;
            for (var i = 0; i < l; i++) {
                if (accumulateList[i].id == id) {
                    accumulateList[i].count++;
                    return accumulateList[i].count;
                }
            }

            accumulateList.push({ id: id, count: 1 });
            return 1;
        }

        function cleanAccumulate(id) {
            var l = accumulateList.length;
            for (var i = 0; i < l; i++) {
                if (accumulateList[i].id == id) {
                    accumulateList.splice(i, 1);
                    return;
                }
            }
        }

        //begin to check accumulating effect
        node.bind('accumulate', function (event, sender, e) {
            var count = accumulate(e.id);
            if (count >= e.max) {
                effect(e.buff);
            }
            if (count == 1) {
                pv.setTimer(node, null, function () {
                    if (!e.isImpacted(node, { position: position })) {
                        cleanAccumulate(e.id);
                        return true;
                    }
                });
            }
        });
        //#endregion

        //buff
        var buffqueue = [];

        function removeBuff(id) {
            var bl = buffqueue.length;
            var buff = $.extend({}, defaultStatus);
            for (var i = 0; i < bl; i++) {
                if (buffqueue[i].id == id)
                    buffqueue.splice(i, 1);
                else {
                    buff = $.extend(buff, buffqueue[i]);
                }
            }

            return buff;
        }

        function effect(e) {
            var buffType = getType(e.id, buffs);
            var newBuff = buffType.effect(node, {});

            node.status = $.extend(node.status, newBuff);
            var animateNode = buffType.animate.begin(node, {});

            newBuff.id = e.id;
            buffqueue.push(newBuff);

            pv.setInterval(function () {
                node.status = removeBuff(e.id);
                buffType.animate.end(node, { node: animateNode });
            }, e.last, { times: 1 });
        };

        //
        function beginAttack() {
            pv.setInterval(function () {
                if (node.isAlive)
                    opts.attacked(node.status.demage, node);
                else
                    return true;
            }, node.status.attack_interval);
        }

        //    //detect target to trigger hit function
        //    function detectTarget() {
        //        var detectedTargets = [];
        //        
        //    }

        //    pv.setTimer(node, null, detectTarget);

        return node;
    };
})(jQuery);